layui.define(['admin','table','form','layarea','element'],function (exports){
    let $ = layui.$
        ,admin  = layui.admin
        ,view   = layui.view
        ,table = layui.table
        ,form = layui.form
        ,layarea = layui.layarea
        ,element = layui.element
        ,setter = layui.setter

    let model = 'home'
        ,module = ['banner','user']

    const active = {
        banner_form:function (d){
            admin.popup({
                title: '添加/编辑 横幅'
                ,area: ['550px', '400px']
                ,id: model+'-'+module[0]+'-form'
                ,resize: false
                ,success: function(layero, index){
                    view(this.id).render(model+'/form/'+module[0]+'_form', d).done(function(){

                        //监听提交
                        form.on('submit(layuiadmin-app-form-submit)', function(data){
                            var field = data.field; //获取提交的字段

                            admin.req({
                                url:'home/banner/post'
                                ,type: 'post'
                                ,data:field
                                ,done:function (res) {

                                    layui.table.reload('table-home-banner'); //重载表格
                                    layer.close(index); //执行关闭
                                }
                            });
                            return false;
                        });

                    });
                }
            });
        },

        user_form:function (d) {
            admin.popup({
                title: '添加用户'
                ,area: ['500px', '400px']
                ,id: 'home-user-form'
                ,resize: false
                ,success: function(layero, index){
                    view(this.id).render(model+'/form/'+module[1]+'_form', d).done(function(){

                        //监听提交
                        form.on('submit(layuiadmin-app-form-submit)', function(data){
                            var field = data.field; //获取提交的字段

                            admin.req({
                                url:'home/user/post'
                                ,type: 'post'
                                ,data:field
                                ,done:function (res) {

                                    layui.table.reload('table-home-user'); //重载表格
                                    layer.close(index); //执行关闭
                                }
                            });
                            return false;
                        });

                    });
                }
            });
        },
        user_recharge_form:function (d) {
            admin.popup({
                title: '用户充值'
                ,area: ['500px', '400px']
                ,id: 'home-user-recharge-form'
                ,resize: false
                ,success: function(layero, index){
                    view(this.id).render(model+'/form/'+module[1]+'_recharge_form', d).done(function(){

                        //监听提交
                        form.on('submit(layuiadmin-app-form-submit)', function(data){
                            var field = data.field; //获取提交的字段

                            admin.req({
                                url:'home/user/recharge'
                                ,type: 'post'
                                ,data:field
                                ,done:function (res) {

                                    layui.table.reload('table-home-user-recharge'); //重载表格
                                    layer.close(index); //执行关闭
                                }
                            });
                            return false;
                        });

                    });
                }
            });
        }
    }

    //banner
    table.render({
        elem: '#table-'+model+'-'+module[0]
        ,toolbar: '#table-tool'
        ,url: setter.request.path + model+'/'+module[0]
        ,cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
        ,cols: [[
            {field: 'id', width: 80, title: 'ID'}
            ,{field: 'name',title: '名称'}
            ,{field: 'image',title: '图片',templet: '#table-img'}
            ,{field: 'show', width: 120, title: '是否显示',templet:'#table-show'}
            ,{title: '操作', width: 200, align: 'center', fixed: 'right', toolbar: '#app-table-oper'}
        ]]
        ,page:true
    });

    //头部工具栏
    table.on('toolbar(table-'+model+'-'+module[0]+')', function(obj){

        switch(obj.event){
            case 'add':
                active.banner_form();
                break;
        };
    });

    table.on('tool(table-'+model+'-'+module[1]+')', function(obj){
        var data = obj.data;
        if(obj.event === 'all'){
            layer.confirm('确定打开所有门？', function(index){

                admin.req({
                    url: 'home/banner/delete'
                    ,data:data
                    ,done:function (res) {

                        layui.table.reload('table-home-banner');
                        layer.close(index);
                    }
                });
            });
        } else if(obj.event === 'edit'){
            active.user_form(data);
        } else if (obj.event === 'delete'){

            layer.confirm('是否删除横幅',function (index) {


                admin.req({
                    url: 'home/banner/delete'
                    ,data: {id:data.id}
                    ,done: function (res) {

                        layui.table.reload('table-home-banner');
                        layer.close(index);
                    }
                });
            });
        }
    });

    //监听状态工具栏
    form.on('switch(table-'+model+'-'+module[0]+'-status)', function(obj){
        let data = JSON.parse(decodeURIComponent($(this).data('json')));

        admin.req({
            url: model+'/'+module[0]+'/show'
            ,data: {id:data.id,show:obj.elem.checked ?1:0}
        });
    });

    //user
    table.render({
        elem: '#table-'+model+'-'+module[1]
        ,toolbar: '#table-tool'
        ,url: setter.request.path + model+'/'+module[1]
        ,cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
        ,cols: [[
            {field: 'id', width: 80, title: 'ID'}
            ,{field: 'mobile', width:170, title: '手机号'}
            ,{field: 'nickname',title: '名称'}
            ,{field: 'balance',width: 100,title: '积分'}
            ,{field: 'invite', width: 170,title: '邀请人'}
            ,{field: 'created_at',title: '创建时间',width: 180}
            ,{title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#oper-home-user'}
        ]]
        ,page:true
    });

    //头部工具栏
    table.on('toolbar(table-'+model+'-'+module[1]+')', function(obj){

        switch(obj.event){
            case 'add':
                active.user_form();
                break;
        };
    });

    table.on('tool(table-'+model+'-'+module[1]+')', function(obj){
        var data = obj.data;
        if(obj.event === 'del'){

        } else if(obj.event === 'recharge'){
            active.user_recharge_form(data);
        }
    });

    //监听状态工具栏
    form.on('switch(table-'+model+'-'+module[1]+'-status)', function(obj){
        let data = JSON.parse(decodeURIComponent($(this).data('json')));

        admin.req({
            url: model+'/'+module[0]+'/status'
            ,data: {id:data.id,show:obj.elem.checked ?1:0}
        });
    });

    exports('home',active);
});
